<?php
// 配置文件
define('TODO_FILE', 'todos.json');
define('UPLOAD_DIR', 'upload/');

// 读取现有数据
$todos = [];
if (file_exists(TODO_FILE)) {
    $todos = json_decode(file_get_contents(TODO_FILE), true) ?: [];
}

// 获取当前日期
$today = date('Y-m-d');

// 筛选已确认的历史作业：已完成、已确认
$historyTodos = array_filter($todos, function($item) use ($today) {
    return $item['status'] && $item['confirmed'];
})
?>
<!DOCTYPE html>
<html>
<head>
    <style>
        .timer-btn {
            padding: 12px 24px;
            font-size: 1em;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            display: block;
            margin-bottom: 25px;
        }
        .timer-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 8px rgba(0,0,0,0.15);
        }
        .timer-btn:active {
            transform: translateY(0);
        }
        .button-container {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-bottom: 25px;
        }
    </style>
    <title>历史作业</title>
    <style>
        .completed { color: #999; }
        .todo-item { font: 2em sans-serif; margin: 10px 0; padding: 5px; border-bottom: 1px solid #eee; }
        img.preview { max-width: 400px; vertical-align: middle; }
        a { 
            display: inline-block;
            margin-top: 20px;
            color: #333;
            text-decoration: none;
            font-size: 1.2em;
        }
        button { 
            padding: 5px 10px;
            font-size: 0.8em;
            background: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            margin-left: 10px;
        }
    </style>
</head>
<body>
    <h2>历史作业</h2>
    <div class="button-container">
        <a href="index.php" class="timer-btn">返回首页</a>
    </div>
    <?php foreach (array_reverse($historyTodos, true) as $index => $item): ?>
        <div class="todo-item completed">
            <small style="font-size: 0.5em;"><?= $item['date'] ?></small><br>
            <span><?= htmlspecialchars($item['content']) ?></span>
            <?php if (!empty($item['image']) && file_exists(UPLOAD_DIR . $item['image'])): ?>
                <br><a href="<?= UPLOAD_DIR . htmlspecialchars($item['image']) ?>" target="_blank">
                    <img src="<?= UPLOAD_DIR . htmlspecialchars($item['image']) ?>" 
                         class="preview">
                </a>
            <?php endif; ?>
        </div>
    <?php endforeach; ?>
</body>
</html>